package vista;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VentanaConversion extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel jpConversion;
	private JLabel lbltitulo;
	private JLabel lblTipoCorte;
	private JButton btnCancelar;
	private JButton btnBuscar;
	private JButton btnGuardar;
	private JTextField txtFactor;
	private JComboBox cmbTipoCorte;
	private JTextField txtCodigoConversion;
	private JButton btnSalir;
	private JButton btnEliminar;
	private JButton btnModificar;
	private JLabel lblFactor;
	private JLabel lblCodigoConversion;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				VentanaConversion inst = new VentanaConversion();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
/*PATRON SINGLENTON*/
	
	private static VentanaConversion instancia;

	public static  VentanaConversion getInstancia() {
		if (instancia == null){
			instancia = new VentanaConversion();
	}
		return instancia;
	}
	
	public void agregarListener(ActionListener accion){
		
		btnGuardar.addActionListener(accion);
		btnModificar.addActionListener(accion);
		btnEliminar.addActionListener(accion);
		btnCancelar.addActionListener(accion);
		btnBuscar.addActionListener(accion);
		btnSalir.addActionListener(accion);
		}
	
	public void setCodigoConversion(String cod){
		txtCodigoConversion.setText(cod);
	}
	
	public String getCodigoConversion(){
		return txtCodigoConversion.getText();
	}
	
	
	public void setFactor(float factor){
		txtFactor.setText(String.valueOf(factor));
	}
	
	public float getFactor(){
		return Float.valueOf(txtFactor.getText());
	}

	public void setTipoCorte(String tipo){
		cmbTipoCorte.setSelectedItem(tipo);
	}
	
	public String getTipoCorte(){	
	  return cmbTipoCorte.getSelectedItem().toString();
	}
	
	
	
	
	public void setTiposCorteComboBox(ArrayList<String> nombre){
		cmbTipoCorte.removeAllItems();
		cmbTipoCorte.addItem("Seleccione una opcion");
		for(int i = 0; i < nombre.size();i++)
			cmbTipoCorte.addItem(nombre.get(i));
		
		}
	
	public void limpiarCampos(){
		txtCodigoConversion.setText("");
		txtFactor.setText("");
		habilitarBuscar(true);
		habilitarGuardar(true);
		habilitarEliminar(false);
		habilitarModificar(false);
	}
	

	public void habilitarGuardar(boolean x){
		btnGuardar.setEnabled(x);
	}
	
	public void habilitarModificar(boolean x)	{
		btnModificar.setEnabled(x);
	}
	public void habilitarEliminar(boolean x){
		btnEliminar.setEnabled(x);
	}
	public void habilitarCancelar(boolean x){
		btnCancelar.setEnabled(x);
	}
	public void habilitarBuscar(boolean x){
		btnBuscar.setEnabled(x);
	}
	
	public void habilitarCodigoConversion(boolean x){
		txtCodigoConversion.setEnabled(x);
	}
	
	public void mostrarMensaje(String mensaje) {
		JOptionPane.showMessageDialog(this, mensaje);
	}
	
public Boolean validar(){
		
		if (txtCodigoConversion.getText().isEmpty()== true){
			mostrarMensaje("Ingrese el codigo");		
			return false;
		}
		else
			if(cmbTipoCorte.getActionCommand().isEmpty()== true){
			mostrarMensaje("Seleccione el tipo de corte")	;
			return false;
		}	
		else
			if(txtFactor.getText().isEmpty() == true){
			mostrarMensaje("Ingrese el factor");
		      return false;
			}
		 else
		      return true;
		}
	
	public VentanaConversion() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Conversion");
			getContentPane().setLayout(null);
			{
				jpConversion = new JPanel();
				getContentPane().add(jpConversion, "Center");
				jpConversion.setBounds(0, 0, 579, 329);
				jpConversion.setLayout(null);
				jpConversion.setBackground(new java.awt.Color(255,255,255));
				{
					lbltitulo = new JLabel();
					jpConversion.add(lbltitulo);
					lbltitulo.setText("Registro de Conversiones");
					lbltitulo.setBounds(212, 19, 191, 15);
				}
				{
					lblCodigoConversion = new JLabel();
					jpConversion.add(lblCodigoConversion);
					lblCodigoConversion.setText("Codigo de Conversion:");
					lblCodigoConversion.setBounds(31, 72, 170, 15);
				}
				{
					lblTipoCorte = new JLabel();
					jpConversion.add(lblTipoCorte);
					lblTipoCorte.setText("Tipo de Corte:");
					lblTipoCorte.setBounds(31, 118, 115, 15);
				}
				{
					lblFactor = new JLabel();
					jpConversion.add(lblFactor);
					lblFactor.setText("Factor de Conversion: ");
					lblFactor.setBounds(32, 162, 169, 15);
				}
				{
					txtCodigoConversion = new JTextField();
					jpConversion.add(txtCodigoConversion);
					txtCodigoConversion.setBounds(200, 65, 81, 22);
				}
				{
					ComboBoxModel cmbTipoCorteModel = 
							new DefaultComboBoxModel(
									new String[] { "Item One", "Item Two" });
					cmbTipoCorte = new JComboBox();
					jpConversion.add(cmbTipoCorte);
					cmbTipoCorte.setModel(cmbTipoCorteModel);
					cmbTipoCorte.setBounds(200, 111, 153, 22);
				}
				{
					txtFactor = new JTextField();
					jpConversion.add(txtFactor);
					txtFactor.setBounds(200, 159, 90, 22);
				}
				{
					btnBuscar = new JButton();
					jpConversion.add(btnBuscar);
					btnBuscar.setText("Buscar");
					btnBuscar.setBounds(311, 65, 83, 22);
				}
				{
					btnGuardar = new JButton();
					jpConversion.add(btnGuardar);
					btnGuardar.setText("Guardar");
					btnGuardar.setBounds(12, 262, 89, 22);
				}
				{
					btnCancelar = new JButton();
					jpConversion.add(btnCancelar);
					jpConversion.add(getBtnModificar());
					jpConversion.add(getBtnEliminar());
					jpConversion.add(getBtnSalir());
					btnCancelar.setText("Limpiar");
					btnCancelar.setBounds(359, 262, 85, 22);
				}
			}
			pack();
			this.setSize(588, 358);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private JButton getBtnModificar() {
		if(btnModificar == null) {
			btnModificar = new JButton();
			btnModificar.setText("Modificar");
			btnModificar.setBounds(121, 262, 95, 22);
		}
		return btnModificar;
	}
	
	private JButton getBtnEliminar() {
		if(btnEliminar == null) {
			btnEliminar = new JButton();
			btnEliminar.setText("Eliminar");
			btnEliminar.setBounds(237, 262, 98, 22);
		}
		return btnEliminar;
	}
	
	private JButton getBtnSalir() {
		if(btnSalir == null) {
			btnSalir = new JButton();
			btnSalir.setText("Cancelar");
			btnSalir.setBounds(464, 262, 93, 22);
		}
		return btnSalir;
	}

}
